#include "QtGraphicsScene.h"
#include "QtAnimGraphicsItem.h"
#include "qtbuttonpropertybrowser.h"
#include "qttreepropertybrowser.h"
#include "qtgroupboxpropertybrowser.h"
#include <QtGui>

QtGraphicsScene::QtGraphicsScene(QObject* parent/* = 0*/):
	QGraphicsScene(parent)
{
	connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChange()));
}

QtGraphicsScene::~QtGraphicsScene(void)
{
}

void QtGraphicsScene::newFile()
{

}

void QtGraphicsScene::openFile()
{
	QFileDialog dialog;

	QStringList filters;
	filters << "anim files (*.am)"
		<< "Any files (*)";

	dialog.setNameFilters(filters);
	dialog.setAcceptMode(QFileDialog::AcceptOpen);
	if(dialog.exec())
	{
		mFileName = dialog.selectedFiles().at(0);
	}

	if (mFileName.isEmpty())
	{
		return;
	}

	clear();

	QFile file(mFileName);
	file.open(QIODevice::ReadOnly);
	QDataStream dataStream(&file);

	loadItemsFromDataStream(dataStream, true);
}



QList<QtAnimGraphicsItem*> QtGraphicsScene::loadItemsFromDataStream(QDataStream& dataStream,
																  bool bLoadFromFile /*= false*/,
																  bool bPaste /*= false*/)
{
	QList<QtAnimGraphicsItem*> loadItems;
	while(dataStream.atEnd() == false)
	{
		QString itemType;
		dataStream >> itemType;

		QtAnimGraphicsItem* item = 0;

		if (itemType == "QtAnimGraphicsItem")
		{
			item = new QtAnimGraphicsItem();
			addItem(item);
			//addItem(item);
			dataStream >> *item;
		}
		if (item)
		{
			loadItems.push_back(item);
		}
	}
	return loadItems;
}

void QtGraphicsScene::saveFile()
{
	if (mFileName.isEmpty())
	{
		QFileDialog dialog;
		dialog.setDefaultSuffix("am");
		dialog.setViewMode(QFileDialog::Detail);
		dialog.setAcceptMode(QFileDialog::AcceptSave);
		QStringList filters;
		filters << "anim files (*.am)"
			<< "Any files (*)";

		dialog.setNameFilters(filters);
		if(dialog.exec())
		{
			mFileName = dialog.selectedFiles().at(0);
		}
	}
	if (mFileName.isEmpty())
	{
		return;
	}

	QFile file(mFileName);
	file.open(QIODevice::WriteOnly);
	QDataStream dataStream(&file);

	QGraphicsItem* item;
	foreach(item, items())
	{
		QtAnimGraphicsItem* boxItem = dynamic_cast<QtAnimGraphicsItem*>(item);
		if (boxItem)
		{
			dataStream << *boxItem;
		}
	}

}

void QtGraphicsScene::saveFileAs()
{
	QFileDialog dialog;

	QStringList filters;
	filters << "qb2 files (*.qb2)"
		<< "Any files (*)";

	dialog.setDefaultSuffix("qb2");
	dialog.setViewMode(QFileDialog::Detail);
	dialog.setNameFilters(filters);
	dialog.setAcceptMode(QFileDialog::AcceptSave);
	if(dialog.exec())
	{
		mFileName = dialog.selectedFiles().at(0);
	}

	if (mFileName.isEmpty())
	{
		return;
	}

	saveFile();

}

void QtGraphicsScene::createItem()
{
	QtAnimGraphicsItem* item = new QtAnimGraphicsItem;
	addItem(item);
}

void QtGraphicsScene::selectionChange()
{
	QList<QGraphicsItem*> items = selectedItems();
	if (items.size() > 0)
	{
		QtAnimGraphicsItem* pItem = dynamic_cast<QtAnimGraphicsItem*>( items.at(0) );
		
		if (pItem)
		{
			emit changeProperties(pItem->getPropertyWidget());
			emit changeStateScene(pItem->getStateScene());
		}
	}
}